package common;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import object.model.MySQL_TableDDL;
import object.model.TableType;

import system.mysql.schema.information.models.VIEWS;

public class ViewSorter {
	
	public List<MySQL_TableDDL> getSortedViewsDDL(List<MySQL_TableDDL> ddlList){
		List<VIEWS> viewList = this.toVIEWS(ddlList);
		viewList = this.getSortedViews(viewList);
		ddlList = this.toDDL(viewList);
		return ddlList;
	}
	
	public List<VIEWS> getSortedViews(List<VIEWS> viewList){
		List<VIEWS> sortedView = new ArrayList<VIEWS>();
		List<String> needSorted = this.getNames(viewList);
		List<String> needRemove = new ArrayList<String>();		
		while(needSorted.size()!=0){
			List<VIEWS> oriL = this.genOriL(needSorted, viewList);
			for(int i=0;i<oriL.size();i++){
				VIEWS vI = oriL.get(i);
				List<String> relL = this.getRelatedViewName(vI, needSorted);
				if(relL.size()!=0){
					//
				}else{
					sortedView.add(vI);
					needRemove.add(vI.getTABLE_NAME());
				}
			}
			needSorted.removeAll(needRemove);
			needRemove.clear();
		}
		return sortedView;
	}
	
	private List<VIEWS> genOriL(List<String> needSorted, List<VIEWS> vL){
		List<VIEWS> oriL = new ArrayList<VIEWS>();
		for(int i=0;i<vL.size();i++){
			if(needSorted.contains(vL.get(i).getTABLE_NAME())){
				oriL.add(vL.get(i));
			}
		}
		return oriL;
	}
	
	private List<String> getRelatedViewName(VIEWS view, List<String> viewList){
		List<String> relViewName = new ArrayList<String>();
		String def = view.getVIEW_DEFINITION();		
		Pattern p = Pattern.compile("`\\w+`\\.`\\w+`[(\\s{1}AS\\s{1})|(\\.)]`\\w+`");
		Matcher m = p.matcher(def);
		while(m.find()){
			String str = m.group();
			String relName = this.getRelView(str, viewList);
			if(relName!=null && !relViewName.contains(relName)){
				relViewName.add(relName);
			}
		}
		return relViewName;
	}
	
	private String getRelView(String def,List<String> viewList){
		for(int i=0;i<viewList.size();i++){
			String viewN = viewList.get(i);
			if(def.contains(viewN)){
				return viewN;
			}
		}
		return null;
	}
	
	public List<String> getNames(List<VIEWS> viewList) {
		// TODO Auto-generated method stub
		List<String> nameLi = new ArrayList<String>();
		for(int i=0;i<viewList.size();i++){ 
			nameLi.add(viewList.get(i).getTABLE_NAME());
		}
		return nameLi;
	}	
	
	private List<MySQL_TableDDL> toDDL(List<VIEWS> views){
		List<MySQL_TableDDL> ddlList = new ArrayList<MySQL_TableDDL>();
		for(int i=0;i<views.size();i++){
			VIEWS view = views.get(i);
			MySQL_TableDDL ddl = new MySQL_TableDDL(view.getTABLE_NAME(), TableType.VIEW);
			ddl.setCreateTable(view.getVIEW_DEFINITION());		
			ddlList.add(ddl);
		}
		return ddlList;
	}
	
	private List<VIEWS> toVIEWS(List<MySQL_TableDDL> ddlList){
		List<VIEWS> views = new ArrayList<VIEWS>();
		for(int i=0;i<ddlList.size();i++){
			MySQL_TableDDL ddl = ddlList.get(i);
			VIEWS view = new VIEWS();
			view.setTABLE_NAME(ddl.getTable());
			view.setVIEW_DEFINITION(ddl.getCreateTable());
			views.add(view);
		}
		return views;
	}
	
	
}
